package com.legoo.studaychineseonline.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.adapter.ChooseClassContentAdapter;
import com.legoo.studaychineseonline.adapter.ChooseClassContentLevelPopupWindowAdapter;
import com.legoo.studaychineseonline.adapter.ChooseClassContentPopupWindowAdapter;
import com.legoo.studaychineseonline.adapter.ChooseClassContentPopupWindowThirdAdapter;
import com.legoo.studaychineseonline.base.BaseActivity;
import com.legoo.studaychineseonline.bean.CategoryTypeBean;
import com.legoo.studaychineseonline.bean.DifficultListBean;
import com.legoo.studaychineseonline.bean.ListBean;
import com.legoo.studaychineseonline.http.API;
import com.legoo.studaychineseonline.http.XUtil;
import com.legoo.studaychineseonline.utils.IsNetWork;
import com.legoo.studaychineseonline.view.MyCommonPopupWindow;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 选择课程内容
 */
public class ChooseClassContentActivity extends BaseActivity implements MyCommonPopupWindow.ViewInterface, ChooseClassContentPopupWindowAdapter.OnPopupWindowItemClickListener,
        ChooseClassContentPopupWindowThirdAdapter.OnThirdPopupWindowItemClickListener, ChooseClassContentLevelPopupWindowAdapter.OnPopupWindowLevelItemClickListener {

    @BindView(R.id.tv_title_left)
    TextView tvTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    private String classType;//私教 还是小班
    private String where;//试听 还是预约
    private boolean allowTest;//允许试听 可选值有"true", "false"
    private MyCommonPopupWindow popupWindow;
    private MyCommonPopupWindow popupWindow2;
    List<CategoryTypeBean.DataBean.CategoryListBean> categoryListBeans = new ArrayList<>();//一个课程分类
    List<DifficultListBean.DataBean.DifficultyListBean> difficultyListBeans = new ArrayList<>();//难度分类
    List<CategoryTypeBean.DataBean.CategoryListBean.ChildListBean> childListBeans = new ArrayList<>();//二个课程分类
    List<ListBean.DataBean.CourseListBean> course_list = new ArrayList<>();//课程列表
    private ChooseClassContentPopupWindowAdapter chooseClassContentPopupWindowAdapter;
    private ChooseClassContentPopupWindowThirdAdapter cAapter;
    private RecyclerView rvThird;
    private ChooseClassContentAdapter chooseClassContentAdapter;

    @Override
    protected void myOnCreate(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        if (IsNetWork.isNetworkAvailable(context)) {
            showProgressDialog();
            getClassType();
            getLevel();
            getClassList("", "");
        } else {
            showToast("请检查您的网络环境");
        }
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        classType = intent.getStringExtra("classType");
        where = intent.getStringExtra("where");
        //预约课
        if ("buy".equals(where)) {
            allowTest = false;
        }
        //试听课
        if ("audition".equals(where)) {
            allowTest = true;
        }
        tvTitle.setText("选择试听内容");
        tvTitleLeft.setBackgroundResource(R.mipmap.img_fanhui);
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) tvTitleRight.getLayoutParams();
        linearParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
        linearParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        tvTitleRight.setLayoutParams(linearParams);
        tvTitleRight.setBackgroundResource(0);
        tvTitleRight.setGravity(Gravity.CENTER);
        tvTitleRight.setText("下一步");
        rvList.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        chooseClassContentAdapter = new ChooseClassContentAdapter(context,course_list);
        rvList.setAdapter(chooseClassContentAdapter);
    }

    @Override
    protected void resolveBundle(Bundle bundle) {

    }

    @Override
    protected int myLayoutId() {
        return R.layout.activity_choose_content;
    }


    @OnClick({R.id.tv_title_left, R.id.tv_title_right, R.id.tv_type, R.id.tv_level})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_title_left:
                finish();
                break;
            case R.id.tv_title_right:
                startIntent(ChoosClassTimeActivity.class);
                break;
            case R.id.tv_type:
                typePopWindows(view);
                break;
            case R.id.tv_level:
                levelPopWindows(view);
                break;
            default:
        }
    }

    /**
     * 课程类型弹出框
     */
    public void typePopWindows(View view) {
//        if (popupWindow != null && popupWindow.isShowing()) {
//            return;
//        }
        popupWindow = new MyCommonPopupWindow.Builder(this)
                .setView(R.layout.popup_class_type)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimationStyle(R.style.AnimDown)
                .setBackGroundLevel(0.9f)
                .setViewOnclickListener(this)
                .setOutsideTouchable(true)
                .create();
        popupWindow.showAsDropDown(view);
    }

    /**
     * 课程难度弹出框
     */
    public void levelPopWindows(View view) {
//        if (popupWindow2 != null && popupWindow2.isShowing()) {
//            return;
//        }
        popupWindow2 = new MyCommonPopupWindow.Builder(this)
                .setView(R.layout.popup_level)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimationStyle(R.style.AnimDown)
                .setBackGroundLevel(0.9f)
                .setViewOnclickListener(this)
                .setOutsideTouchable(true)
                .create();
        popupWindow2.showAsDropDown(view);
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.popup_class_type:
                RecyclerView rvSecond = (RecyclerView) view.findViewById(R.id.rv_second);
                rvThird = (RecyclerView) view.findViewById(R.id.rv_third);
                rvSecond.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                rvThird.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                chooseClassContentPopupWindowAdapter = new ChooseClassContentPopupWindowAdapter(context, categoryListBeans);
                chooseClassContentPopupWindowAdapter.setOnItemClickLitener(this);
                rvSecond.setAdapter(chooseClassContentPopupWindowAdapter);

                break;
            case R.layout.popup_level:
                RecyclerView rvLevel = (RecyclerView) view.findViewById(R.id.rv_level);
                rvLevel.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                ChooseClassContentLevelPopupWindowAdapter levelAdapter = new ChooseClassContentLevelPopupWindowAdapter(context, difficultyListBeans);
                levelAdapter.setOnItemClickLitener(this);
                rvLevel.setAdapter(levelAdapter);
                break;
            default:
        }
    }

    private void getClassType() {
        categoryListBeans.clear();
        Map<String, Object> map = new HashMap();
        map.put("category_type", "live");
        XUtil.Post(API.CATEGORY_LIST, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                CategoryTypeBean categoryTypeBean = new Gson().fromJson(result, CategoryTypeBean.class);
                if (categoryTypeBean.getCode() == API.HTTP_OK) {
                    categoryListBeans.addAll(categoryTypeBean.getData().getCategory_list());

                }
                hideProgressDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                hideProgressDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getLevel() {
        difficultyListBeans.clear();
        Map<String, Object> map = new HashMap();
        XUtil.Post(API.DIFFICULTY_LIST, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                DifficultListBean difficultListBean = new Gson().fromJson(result, DifficultListBean.class);
                if (difficultListBean.getCode() == API.HTTP_OK) {
                    difficultyListBeans.addAll(difficultListBean.getData().getDifficulty_list());
                }
                hideProgressDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                hideProgressDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 一级类型条目点击事件
     */
    @Override
    public void onSecondPopupWindowItemClick(final View view, final TextView textView, final int position) {
        childListBeans.clear();
        childListBeans.addAll(categoryListBeans.get(position).getChild_list());
        cAapter = new ChooseClassContentPopupWindowThirdAdapter(context, childListBeans);
        cAapter.setOnItemClickLitener(this);
        rvThird.setAdapter(cAapter);


    }

    /**
     * 二级类型条目点击事件
     */
    @Override
    public void onThirdPopupWindowItemClick(View view, TextView textView, final int position) {
        tvType.setText(childListBeans.get(position).getName_zh());
        popupWindow.dismiss();
    }

    /**
     * 难度条目点击事件
     */
    @Override
    public void onPopupWindowLevelItemClick(View view, TextView textView, int position) {
        tvLevel.setText(difficultyListBeans.get(position).getName_en());
        popupWindow2.dismiss();
    }

    private void getClassList(String categoryId, String difficultyId) {
        course_list.clear();
        Map<String, Object> map = new HashMap();
        map.put("course_type", classType);
        map.put("allow_test", allowTest);
        if (!"".equals(categoryId)) {
            map.put("category_id", categoryId);
        }
        if (!"".equals(categoryId)) {
            map.put("difficulty_id", difficultyId);
        }
        XUtil.Post(API.LIST, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                ListBean listBean = new Gson().fromJson(result,ListBean.class);
                if (listBean.getCode()==API.HTTP_OK){
                    course_list.addAll(listBean.getData().getCourse_list());
                    chooseClassContentAdapter.notifyDataSetChanged();
                }
                hideProgressDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                hideProgressDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
